<template>
  <view>
    <!--页面标题头begin-->
    <view class="smart-page-head">
      <view class="smart-page-head-title">form表单</view>
    </view>
    <!--页面标题头end-->
    <view class="uni-padding-wrap uni-common-mt">
      <form @submit="formSubmit" @reset="formReset">
        <view class="uni-form-item uni-column">
          <view class="title">姓名</view>
          <input class="uni-input" name="nickname" placeholder="请输入姓名" />
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">性别</view>
          <radio-group name="gender">
            <label>
              <radio value="男" /><text>男</text>
            </label>
            <label>
              <radio value="女" /><text>女</text>
            </label>
          </radio-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">爱好</view>
          <checkbox-group name="loves">
            <label>
              <checkbox value="读书" /><text>读书</text>
            </label>
            <label>
              <checkbox value="写字" /><text>写字</text>
            </label>
            <label>
              <checkbox value="运动" /><text>运动</text>
            </label>
          </checkbox-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">年龄</view>
          <slider value="20" name="age" show-value></slider>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">邮箱</view>
          <input class="uni-input" name="email" placeholder="请输入邮箱" />
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">保留选项</view>
          <view>
            <switch name="switch" />
          </view>
        </view>
        <view class="uni-btn-v">
          <button form-type="submit">提交</button>
          <button type="default" form-type="reset">重置</button>
        </view>
      </form>
    </view>
  </view>
</template>

<script>
// 使用require导入CommonJS模块
import graceChecker from "../../../lib/js/graceChecker.js";

export default {
  data() {
    return {
    }
  },
  methods: {
    formSubmit: function(e) {
      console.log('表单提交数据：' + JSON.stringify(e.detail.value))
      
      // 定义表单验证规则
      var rule = [
        {name: "nickname", checkType: "string", checkRule: "1,5", errorMsg: "姓名应为1-5个字符"},
        {name: "gender", checkType: "in", checkRule: "男,女", errorMsg: "请选择性别"},
        {name: "loves", checkType: "notnull", checkRule: "", errorMsg: "请至少选择一项爱好"},
        {name: "email", checkType: "email", checkRule: "", errorMsg: "请输入有效的邮箱地址"}
      ];
      
      // 进行表单检查
      var formData = e.detail.value;
      var checkRes = graceChecker.check(formData, rule);

      if (checkRes) {
        uni.showToast({title: "验证通过!", icon: "none"});
        // 这里可以添加表单提交成功后的逻辑
      } else {
        uni.showToast({ title: graceChecker.error, icon: "none" });
      }
    },
    formReset: function(e) {
      console.log('表单已重置');
      uni.showToast({title: "表单已重置", icon: "none"});
    }
  }
}
</script>

<style>
.smart-page-head {
  padding: 16px;
  background-color: #f8f8f8;
  border-bottom: 1px solid #eee;
}

.smart-page-head-title {
  font-size: 18px;
  text-align: center;
  font-weight: bold;
}

.uni-padding-wrap {
  padding: 16px;
}

.uni-common-mt {
  margin-top: 10px;
}

.uni-form-item {
  margin-bottom: 20px;
}

.title {
  margin-bottom: 8px;
  font-size: 14px;
  color: #333;
}

.uni-input {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

radio-group, checkbox-group {
  display: flex;
  gap: 16px;
  padding: 10px 0;
}

.uni-btn-v {
  display: flex;
  gap: 10px;
  margin-top: 30px;
}

button {
  flex: 1;
  padding: 12px;
  border-radius: 4px;
  font-size: 16px;
}

button:first-child {
  background-color: #007aff;
  color: white;
  border: none;
}

button[type="default"] {
  background-color: white;
  color: #333;
  border: 1px solid #ddd;
}
</style>
